#include "layoutgenomeelements.h"

#include "../../VisBaseClasses/visbaseelement.h"

LayoutGenomeElements::LayoutGenomeElements(QObject *parent) :
    VisBaseLayout(parent)
{
    init();
}

LayoutGenomeElements::LayoutGenomeElements(QList< QPointer< VisBaseElement > > e, QObject *parent) :
    VisBaseLayout(parent)
{
    elementsToLayout.append(e);

    init();
}

//LayoutGenomeElements::LayoutGenomeElements()

void LayoutGenomeElements::init()
{
    startNt = 0;
    stopNt = 10000;
    startX = 0.0;
    stopX = 1200;
}

void LayoutGenomeElements::setStartNt(int s)
{
    startNt = s;
}

void LayoutGenomeElements::setStopNt(int s)
{
    stopNt = s;
}

void LayoutGenomeElements::setStartX(int s)
{
    startX = s;
}

void LayoutGenomeElements::setStopX(int s)
{
    stopX = s;
}

void LayoutGenomeElements::setNtPerPixel(float ntPerPixel)
{
    setStopNt( ntPerPixel*(stopX-startX)+startNt);
}

void LayoutGenomeElements::process()
{
    //qDebug() <<"layout genome elements " << elementsToLayout.size() << endl;

    for(int i = 0 ; i < elementsToLayout.size(); i++)
    {
//        qDebug() << getGenomeElement(i)->getStart() << endl;
//        qDebug() << getGenomeElement(i)->getStop() << endl;

        GenomeElement* ge = getGenomeElement(i);
        int geneStartX = map( ge->getStart() );
        int geneStopX = map( ge->getStop() );

        //qDebug() << "       " << geneStartX << " " << geneStopX << endl;

        ge->setX((float)geneStartX);
        ge->setWidth((float)geneStopX-geneStartX);
    }
    //qDebug() << "done laying out " << endl;
}

int LayoutGenomeElements::map(int nt)
{
    return (int) (( (float)(nt-startNt) )/( (float)(stopNt-startNt) ) * (stopX-startX) + startX);
}


GenomeElement* LayoutGenomeElements::getGenomeElement(int i)
{
    VisBaseElement* vbe = elementsToLayout[i];
    GenomeElement* ge = dynamic_cast< GenomeElement* > ( vbe );
    return ge;
}


